﻿using Sidvall.Data;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Sidvall.UI.Entities
{
    public static class SelectableKeyValueManager
    {
        #region Public Members

        #region Create

        public static SelectableKeyValueCollection<string, string> Create(bool isSelected, IEnumerable<IItem> items)
        {
            var values = new SelectableKeyValueCollection<string, string>();
            if (items == null)
                return values;
            foreach (var item in items)
                values.Add(isSelected, item.Id, item.Name, item);
            return values;
        }
        public static SelectableKeyValueCollection<string, string> Create(bool isSelected, params IItem[] items)
        {
            return Create(isSelected, items.AsEnumerable());
        }

        #endregion
        #region CreateFromEnum

        public static SelectableKeyValueCollection<string, TValue> CreateFromEnum<TValue>(bool isSelected)
        {
            return CreateFromEnum<TValue>(o =>
            {
                return isSelected;
            });
        }
        public static SelectableKeyValueCollection<string, TValue> CreateFromEnum<TValue>(int selectedValues)
        {
            return CreateFromEnum<TValue>(o =>
            {
                if ((selectedValues & o) == o)
                    return true;
                else
                    return false;
            });
        }
        private static SelectableKeyValueCollection<string, TValue> CreateFromEnum<TValue>(System.Func<int, bool> selectedHandler)
        {
            var enumType = typeof(TValue);
            var items = new SelectableKeyValueCollection<string, TValue>();
            var values = System.Enum.GetValues(enumType);
            var names = System.Enum.GetNames(enumType);
            var iMax = values.Length;
            for (int i = 0; i < iMax; i++)
            {
                var value = (int)values.GetValue(i);
                var isSelected = selectedHandler(value);
                var key = names[i];
                var fieldInfo = enumType.GetTypeInfo().GetDeclaredField(key);
                var attribute = fieldInfo.GetCustomAttribute(typeof(Sidvall.Serialization.DescriptionAttribute), false);
                if (attribute != null)
                    key = ((Sidvall.Serialization.DescriptionAttribute)attribute).Description;
                items.Add(isSelected, key, (TValue)values.GetValue(i));
            }
            return items;
        }

        #endregion
        #region CreateFromValues

        public static SelectableKeyValueCollection<string, TValue> CreateFromValues<TValue>(bool isSelected, IEnumerable<TValue> items)
        {
            var values = new SelectableKeyValueCollection<string, TValue>();
            if (items == null)
                return values;
            foreach (var item in items)
                values.Add(isSelected, item.ToString(), item);
            return values;
        }
        public static SelectableKeyValueCollection<string, TValue> CreateFromValues<TValue>(bool isSelected, params TValue[] items)
        {
            return CreateFromValues(isSelected, items.AsEnumerable());
        }

        #endregion

        #endregion
    }
}
